<template>
	<div class="default-main">
		<cus-form-title :label="$t('system.cache.cacheManagement')" icon="local-cus-cache"></cus-form-title>
		<cus-table-title :label="$t('system.cache.allCache')" icon="local-cus-cache" style="margin-top: 50px"/>
		<el-row>
			<el-button :loading="buttonLoading" icon="el-icon-Timer" style="width: 200px" type="primary"
			           @click="updateAllCache">
				{{ $t('system.cache.updateAllCache') }}
			</el-button>
		</el-row>
		<cus-table-title :label="$t('system.cache.cacheDetails')" icon="local-cus-cache" style="margin-top: 50px"/>
		<el-row justify="start">
			<el-button :loading="buttonLoading" icon="el-icon-Timer" style="width: 200px" type="primary"
			           @click="cacheSysUser">
				{{ $t('system.cache.updateUserCache') }}
			</el-button>
			<el-button :loading="buttonLoading" icon="el-icon-Timer" style="width: 200px" type="primary"
			           @click="cacheSysUserRole">
				{{ $t('system.cache.updateUserRoleCache') }}
			</el-button>
			<el-button :loading="buttonLoading" icon="el-icon-Timer" style="width: 200px" type="primary"
			           @click="cacheSysRoleAuthority">
				{{ $t('system.cache.updateRoleAuthorityCache') }}
			</el-button>
			<el-button :loading="buttonLoading" icon="el-icon-Timer" style="width: 200px" type="primary"
			           @click="cacheSysAuthority">
				{{ $t('system.cache.updateAuthorityCache') }}
			</el-button>
			<el-button :loading="buttonLoading" icon="el-icon-Timer" style="width: 200px" type="primary"
			           @click="cacheSysBusinessCode">
				{{ $t('system.cache.updateBusinessCodeCache') }}
			</el-button>
		</el-row>
	</div>
</template>

<script lang="ts" name="CacheManagement" setup>

import {
	cacheSysAuthorityApi,
	cacheSysBusinessCodeApi,
	cacheSysRoleAuthorityApi,
	cacheSysUserApi,
	cacheSysUserRoleApi,
	updateAllCacheApi
} from '@/api/system/cache.api'
import {useBusinessCodeStore} from '@/stores/modules/businessCode.store'
import {messageSuccess} from '@/utils/commonUtil'
import {ref} from 'vue'
import CusTableTitle from '@/components/cus-table-title.vue'
import CusFormTitle from '@/components/cus-form-title.vue'

const businessCodeStore = useBusinessCodeStore()
const buttonLoading = ref(false)

const updateAllCache = () => {
	buttonLoading.value = true
	updateAllCacheApi().then(() => {
		businessCodeStore.fetchBusinessCodes().then(() => {
			messageSuccess('所有缓存已更新')
		}).finally(() => {
			buttonLoading.value = false
		})
	}).catch(() => {
		buttonLoading.value = false
	})
}

const cacheSysUser = () => {
	buttonLoading.value = true
	cacheSysUserApi().then(() => {
		messageSuccess('用户缓存已更新')
	}).finally(() => {
		buttonLoading.value = false
	})
}

const cacheSysUserRole = () => {
	buttonLoading.value = true
	cacheSysUserRoleApi().then(() => {
		messageSuccess('用户-角色缓存已更新')
	}).finally(() => {
		buttonLoading.value = false
	})
}

const cacheSysRoleAuthority = () => {
	buttonLoading.value = true
	cacheSysRoleAuthorityApi().then(() => {
		messageSuccess('角色-权限缓存已更新')
	}).finally(() => {
		buttonLoading.value = false
	})
}

const cacheSysAuthority = () => {
	buttonLoading.value = true
	cacheSysAuthorityApi().then(() => {
		messageSuccess('权限缓存已更新')
	}).finally(() => {
		buttonLoading.value = false
	})
}

const cacheSysBusinessCode = () => {
	buttonLoading.value = true
	cacheSysBusinessCodeApi().then(() => {
		businessCodeStore.fetchBusinessCodes().then(() => {
			messageSuccess('业务编码缓存已更新')
		}).finally(() => {
			buttonLoading.value = false
		})
	}).catch(() => {
		buttonLoading.value = false
	})
}

</script>

<style lang="scss" scoped>
</style>
